home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 January / PSL Monthly Shareware CD-ROM (Public Software Library) (January 1994).iso / games / dos / board / backgam.com / SCREEN.C < prev   
Encoding:
C/C++ Source or Header  |  1979-11-30  |  13.5 KB  |  514 lines

  1. /*P*/
  2. /****************************************************************/
  3. /*                                */
  4. /* screen.c                            */
  5. /*                                */
  6. /* These are the routines for updating the display - we draw    */
  7. /* the board, and the dice, etc. in these modules. A nice    */
  8. /* future enhancement is to make the pieces actually "move" on    */
  9. /* the board from point to point. I started, but never finished    */
  10. /* so this is the result - they "jump" instead.            */
  11. /*                                */
  12. /****************************************************************/
  13.  
  14. #include    <stdio.h>
  15. #include    "bkg.h"
  16.  
  17. /****************************************************************/
  18. /*                                */
  19. /* draw                                */
  20. /*                                */
  21. /* This function puts the board up on the screen. It is similar    */
  22. /* in positioning on both the PC and the Kaypro. The difference    */
  23. /* is in the way things are outlined - on the Kaypro we use the    */
  24. /* primitive graphics on the display. On the PC we use the    */
  25. /* character graphics to outline things. We could use the PC in    */
  26. /* color graphics mode, which would enable us to draw actual    */
  27. /* round players, and triangular points. The problem is that    */
  28. /* then we're limited in how many colors we can use... This    */
  29. /* may be a future enhancement, also... And! I should add some    */
  30. /* support for VT-100's, so I can run it on the VAX, and the    */
  31. /* VT-220, which has nice graphics that I know nothing about.    */
  32. /*                                */
  33. /****************************************************************/
  34.  
  35. #define        WIDTH        58
  36. #define        HEIGHT        15
  37. #define        BS        0x08
  38. #define        LF        0x0a
  39.  
  40. draw( side )
  41.  
  42. int    side;
  43.  
  44.     {
  45.  
  46.     int    i, j, disp;
  47.     char    line[ WIDTH + 2 ];
  48.  
  49.     clear();
  50.  
  51. #ifdef    KAYPRO
  52.  
  53.     /* Just draw the lines...    */
  54.  
  55.     set_line( 5, 0, 5, 116 );
  56.     set_line( 61, 0, 61, 116 );
  57.     set_line( 5, 0, 60, 0 );
  58.     set_line( 5, 52, 60, 52 );
  59.     set_line( 5, 64, 60, 64 );
  60.     set_line( 5, 116, 60, 116 );
  61.  
  62.     set_line( X_DIE1 * 4 + 1, Y_DIE1 * 2, X_DIE1 * 4 + 1, Y_DIE1 * 2 + 18 );
  63.     set_line( X_DIE1 * 4 + 17, Y_DIE1 * 2, X_DIE1 * 4 + 17, Y_DIE1 * 2 + 18 );
  64.     set_line( X_DIE1 * 4 + 2, Y_DIE1 * 2, X_DIE1 * 4 + 16, Y_DIE1 * 2 );
  65.     set_line( X_DIE1 * 4 + 2, Y_DIE1 * 2 + 18, X_DIE1 * 4 + 16, Y_DIE1 * 2 + 18 );
  66.  
  67.     set_line( X_DIE2 * 4 + 1, Y_DIE2 * 2, X_DIE2 * 4 + 1, Y_DIE2 * 2 + 18 );
  68.     set_line( X_DIE2 * 4 + 17, Y_DIE2 * 2, X_DIE2 * 4 + 17, Y_DIE2 * 2 + 18 );
  69.     set_line( X_DIE2 * 4 + 2, Y_DIE2 * 2, X_DIE2 * 4 + 16, Y_DIE2 * 2 );
  70.     set_line( X_DIE2 * 4 + 2, Y_DIE2 * 2 + 18, X_DIE2 * 4 + 16, Y_DIE2 * 2 + 18 );
  71.  
  72. #endif
  73.  
  74. #ifdef    IBMPC
  75.  
  76.     /* fill the top and bottom line buffers    */
  77.     /* These hex codes are hard-coded,    */
  78.     /* which is bad, but. See the hardware    */
  79.     /* reference for more info.        */
  80.  
  81.     for( i = 1; i < WIDTH; i++ )
  82.         line[ i ] = 0xcd;
  83.     line[ 0 ] = 0xc9;
  84.     line[ WIDTH ] = 0xbb;
  85.     line[ 26 ] = 0xcb;
  86.     line[ 32 ] = 0xcb;
  87.     line[ WIDTH + 1 ] = '\0';
  88.  
  89.     write_at( 1, 0, line, BOARD_COLOR );
  90.  
  91.     line[ 0 ] = 0xc8;
  92.     line[ WIDTH ] = 0xbc;
  93.     line[ 26 ] = 0xca;
  94.     line[ 32 ] = 0xca;
  95.  
  96.     write_at( 15, 0, line, BOARD_COLOR );
  97.  
  98.     for( i = 1; i < WIDTH; i++ )
  99.         line[ i ] = ' ';
  100.  
  101.     line[ 0 ] = line[ 26 ] = line[ 32 ] = line[ WIDTH ] = 0xba;
  102.     for( i = 2; i < HEIGHT; i++ )
  103.         write_at( i, 0, line, BOARD_COLOR );
  104.  
  105.     /* now do the die boxes */
  106.  
  107.     for( i = 1; i < 8; i++ )
  108.         line[ i ] = 0xc4;
  109.  
  110.     line[ 0 ] = 0xda;
  111.     line[ 8 ] = 0xbf;
  112.     line[ 9 ] = 0x00;
  113.     
  114.     write_at( X_DIE1, Y_DIE1, line, DYE );
  115.     write_at( X_DIE2, Y_DIE2, line, DYE );
  116.  
  117.     line[ 0 ] = 0xc0;
  118.     line[ 8 ] = 0xd9;
  119.  
  120.     write_at( X_DIE1 + 4, Y_DIE1, line, DYE );
  121.     write_at( X_DIE2 + 4, Y_DIE2, line, DYE );
  122.  
  123.     for( i = 1; i < 8; i++ )
  124.         line[ i ] = ' ';
  125.  
  126.     line[ 0 ] = line[ 8 ] = 0xb3;
  127.  
  128.     for( i = 1; i <= 3; i++ )
  129.         {
  130.         write_at( X_DIE2 + i, Y_DIE2, line, DYE );
  131.         write_at( X_DIE1 + i, Y_DIE1, line, DYE );
  132.         } /* loopski */
  133.  
  134. #endif
  135.  
  136. #ifdef    VAX
  137.  
  138.     /* Not tested!!!        */
  139.     /* fill the top and bottom    */
  140.  
  141.     line[ 0 ] = '+';
  142.     for( i = 1; i < WIDTH; i++ )
  143.         line[ i ] = '-';
  144.     line[ WIDTH ] = '+';
  145.     line[ WIDTH + 1 ] = '\0';
  146.  
  147.     write_at( 1, 0, line );
  148.     write_at( 15, 0, line );
  149.  
  150.     for( psn = i = 0; i < HEIGHT; i++ )
  151.         {
  152.         line[ psn++ ] = '|';
  153.         line[ psn++ ] = LF;
  154.         line[ psn++ ] = BS;
  155.         }
  156.  
  157.     line[ psn ] = '\0';
  158.  
  159.     write_at( 2, 0, buf );
  160.     write_at( 2, 26, buf );
  161.     write_at( 2, 32, buf );
  162.     write_at( 2, 58, buf );
  163.  
  164.     /* now do the die boxes */
  165.  
  166.     write_at( X_DIE1, Y_DIE1, "+-------+" );
  167.     for( i = 1; i <= 3; i++ )
  168.         write_at( X_DIE1 + i, Y_DIE1, "|       |" );
  169.     write_at( X_DIE1 + 4, Y_DIE1, "+-------+" );
  170.  
  171.     write_at( X_DIE2, Y_DIE2, "+-------+" );
  172.     for( i = 1; i <= 3; i++ )
  173.         write_at( X_DIE2 + i, Y_DIE2, "|       |" );
  174.     write_at( X_DIE2 + 4, Y_DIE2, "+-------+" );
  175.  
  176. #endif
  177.  
  178.     /* This is common to everybody...    */
  179.     /* Note that if "side" is true, we    */
  180.     /* draw it with respect to white. Else    */
  181.     /* we draw it with respect to black.    */
  182.  
  183.     for( i = 1; i < 13; i++ )
  184.         {
  185.         sprintf( line, "%2d", ( side ? 12 + i : i ) );
  186.         disp = ( ( i * 4 ) - 2 );
  187.         if ( i > 6 )
  188.             disp += 8;
  189.         write_at( 0, disp, line, NUM_COLOR );
  190.         }
  191.  
  192.     for( i = 13; i < 25; i++ )
  193.         {
  194.         sprintf( line, "%2d", ( side ? i - 12 : i ) );
  195.         disp = ( ( ( 25 - i ) * 4 ) - 2 );
  196.         if ( i < 19 )
  197.             disp += 8;
  198.         write_at( 16, disp, line, NUM_COLOR );
  199.         }
  200.  
  201.     } /* draw */
  202.  
  203. /*P*/
  204. /****************************************************************/
  205. /*                                */
  206. /* show_cube                            */
  207. /*                                */
  208. /* This draws the double cube. If "own" is WHITE, then it is    */
  209. /* on white's side. If own is BLACK, ... Else it is in the    */
  210. /* middle of the board, and either person may double. As an    */
  211. /* engancement, we need to change the absolute line/column    */
  212. /* numbers below so that they are relative to the boards x,y    */
  213. /* position on the display. This would allow for easy movement    */
  214. /* later.                            */
  215. /*                                */
  216. /****************************************************************/
  217.  
  218. show_cube( own, val, side )
  219.  
  220. int    own, val, side;
  221.  
  222.     {
  223.  
  224.     char    buf[ 5 ], *blank_out;
  225.     int    i;
  226.  
  227. #ifdef    IBMPC
  228.  
  229.     /* On a real compiler (non-BDS) we have    */
  230.     /* the powerful C languiage @ its best.    */
  231.  
  232.     static char    line[ 3 ][ 6 ] = { { 0xda, 0xc4, 0xc4, 0xc4, 0xbf, 0x00 },
  233.                        { 0xb3, 0x20, 0x20, 0x20, 0xb3, 0x00 },
  234.                        { 0xc0, 0xc4, 0xc4, 0xc4, 0xd9, 0x00 } };
  235.  
  236. #endif
  237.  
  238.     blank_out = "     ";    /* 5 spaces */
  239.     sprintf( buf, "%2d", val );
  240.  
  241.     if ( side )
  242.         switch( own )
  243.             {
  244.  
  245.             case BLACK:    own = WHITE;
  246.                     break;
  247.  
  248.             case WHITE:    own = BLACK;
  249.                     break;
  250.  
  251.             default:    break;
  252.  
  253.             } /* switch */
  254.     
  255. #ifdef    KAYPRO
  256.  
  257.     switch( own )
  258.         {
  259.  
  260.         case WHITE:    set_line( 9, 54, 9, 62 );
  261.                 set_line( 17, 54, 17, 62 );
  262.                 set_line( 9, 54, 17, 54 );
  263.                 set_line( 9, 62, 17, 62 );
  264.                 write_at( 3, 28, buf, CUBE_COLOR );
  265.                 write_at( 7, 27, blank_out, CUBE_COLOR );
  266.                 write_at( 8, 27, blank_out, CUBE_COLOR );
  267.                 write_at( 9, 27, blank_out, CUBE_COLOR );
  268.                 write_at( 12, 27, blank_out, CUBE_COLOR );
  269.                 write_at( 13, 27, blank_out, CUBE_COLOR );
  270.                 write_at( 14, 27, blank_out, CUBE_COLOR );
  271.                 break;
  272.  
  273.         case BLACK:    set_line( 49, 54, 49, 62 );
  274.                 set_line( 57, 54, 57, 62 );
  275.                 set_line( 49, 54, 57, 54 );
  276.                 set_line( 49, 62, 57, 62 );
  277.                 write_at( 13, 28, buf, CUBE_COLOR );
  278.                 write_at( 2, 27, blank_out, CUBE_COLOR );
  279.                 write_at( 3, 27, blank_out, CUBE_COLOR );
  280.                 write_at( 4, 27, blank_out, CUBE_COLOR );
  281.                 write_at( 7, 27, blank_out, CUBE_COLOR );
  282.                 write_at( 8, 27, blank_out, CUBE_COLOR );
  283.                 write_at( 9, 27, blank_out, CUBE_COLOR );
  284.                 break;
  285.  
  286.         default:    set_line( 29, 54, 29, 62 );
  287.                 set_line( 37, 54, 37, 62 );
  288.                 set_line( 29, 54, 37, 54 );
  289.                 set_line( 29, 62, 37, 62 );
  290.                 write_at( 8, 28, buf, CUBE_COLOR );
  291.                 write_at( 2, 27, blank_out, CUBE_COLOR );
  292.                 write_at( 3, 27, blank_out, CUBE_COLOR );
  293.                 write_at( 4, 27, blank_out, CUBE_COLOR );
  294.                 write_at( 12, 27, blank_out, CUBE_COLOR );
  295.                 write_at( 13, 27, blank_out, CUBE_COLOR );
  296.                 write_at( 14, 27, blank_out, CUBE_COLOR );
  297.                 break;
  298.  
  299.         } /* switch */
  300.  
  301. #endif
  302.  
  303. #ifdef    IBMPC
  304.  
  305.     switch( own )
  306.         {
  307.  
  308.         case WHITE:    for( i = 0; i < 3; i++ )
  309.                     write_at( 2 + i, 27, line[ i ], CUBE_COLOR ); 
  310.                 write_at( 3, 28, buf, CUBE_COLOR );
  311.                 write_at( 7, 27, blank_out, CUBE_COLOR );
  312.                 write_at( 8, 27, blank_out, CUBE_COLOR );
  313.                 write_at( 9, 27, blank_out, CUBE_COLOR );
  314.                 write_at( 12, 27, blank_out, CUBE_COLOR );
  315.                 write_at( 13, 27, blank_out, CUBE_COLOR );
  316.                 write_at( 14, 27, blank_out, CUBE_COLOR );
  317.                 break;
  318.  
  319.         case BLACK:    for( i = 0; i < 3; i++ )
  320.                     write_at( 12 + i, 27, line[ i ], CUBE_COLOR );
  321.                 write_at( 13, 28, buf, CUBE_COLOR );
  322.                 write_at( 2, 27, blank_out, CUBE_COLOR );
  323.                 write_at( 3, 27, blank_out, CUBE_COLOR );
  324.                 write_at( 4, 27, blank_out, CUBE_COLOR );
  325.                 write_at( 7, 27, blank_out, CUBE_COLOR );
  326.                 write_at( 8, 27, blank_out, CUBE_COLOR );
  327.                 write_at( 9, 27, blank_out, CUBE_COLOR );
  328.                 break;
  329.  
  330.         default:    for( i = 0; i < 3; i++ )
  331.                     write_at( 7 + i, 27, line[ i ], CUBE_COLOR );
  332.                 write_at( 8, 28, buf, CUBE_COLOR );
  333.                 write_at( 2, 27, blank_out, CUBE_COLOR );
  334.                 write_at( 3, 27, blank_out, CUBE_COLOR );
  335.                 write_at( 4, 27, blank_out, CUBE_COLOR );
  336.                 write_at( 12, 27, blank_out, CUBE_COLOR );
  337.                 write_at( 13, 27, blank_out, CUBE_COLOR );
  338.                 write_at( 14, 27, blank_out, CUBE_COLOR );
  339.                 break;
  340.  
  341.         } /* switch */
  342.  
  343. #endif
  344.  
  345. #ifdef    VAX
  346.  
  347.     /* Not completed.    */
  348.  
  349. #endif
  350.  
  351.     } /* show_cube */
  352.  
  353.  
  354. /*P*/
  355. /****************************************************************/
  356. /*                                */
  357. /* roll                                */
  358. /*                                */
  359. /* This function rolls the dice, and returns the values on the    */
  360. /* dice to the caller. Then, it formats a message according to    */
  361. /* what was rolled, and prints it. The "col" parameter is set    */
  362. /* to the column after the printed message; this is so we can    */
  363. /* address back to here later if we need to. The variable    */
  364. /* "iter" is the number of loop iterations to do when rolling.    */
  365. /* This is simply to provide some variety in short/long rolls    */
  366. /* of the dice.                            */
  367. /*                                */
  368. /****************************************************************/
  369.  
  370. #define        MAX_ITER    30
  371.  
  372. roll( die_1, die_2, col )
  373.  
  374. int    *die_1, *die_2, *col;
  375.  
  376.     {
  377.  
  378.     int    i, iter;
  379.     char    buf[ 80 ];
  380.  
  381.     /* If we're testing this, then just ask    */
  382.     /* and then display them.        */
  383.  
  384.     if ( r_test )
  385.         {
  386.         char_at( X_INFO, Y_INFO, CTEOL, TEXT_COLOR );
  387.         printf( "die 1 and 2: " );
  388.         scanf( "%d %d", die_1, die_2 );
  389.         *col = 0;
  390.         show_die( 1, *die_1 );
  391.         show_die( 0, *die_2 );
  392.         }
  393.     else
  394.         {
  395.  
  396.         iter = ( rand() % MAX_ITER ) + 1;
  397.  
  398.         for( i = 1; i <= iter; i++ )
  399.             {
  400.             show_die( 1, ( *die_1 = ( rand() % 6 ) + 1 ) );
  401.             show_die( 0, ( *die_2 = ( rand() % 6 ) + 1 ) );
  402.             } /* for i */
  403.         }
  404.  
  405.     if ( *die_1 == *die_2 )
  406.         sprintf( buf, "%s rolls double %d's%c", NAME, *die_1, CTEOL );
  407.      else
  408.         sprintf( buf, "%s rolls a %d and a %d%c", NAME, *die_1, *die_2, CTEOL );
  409.  
  410.     write_at( X_INFO + ( me == WHITE ? 0 : 1 ), Y_INFO, buf, TEXT_COLOR );
  411.     *col = strlen( buf ) - 1;
  412.  
  413.     } /* roll */
  414.  
  415. /*P*/
  416. /****************************************************************/
  417. /*                                */
  418. /* show_die                            */
  419. /*                                */
  420. /* The "show_die" function should be apparent. We display one    */
  421. /* or the other die using spaces and 'O''s. The only quirks are    */
  422. /* that some rolls can be displayed 90 degrees rotated, so we    */
  423. /* use "mirror" to do this. (Not a completely accurate variable    */
  424. /* name, come to think of it!)                    */
  425. /*                                */
  426. /****************************************************************/
  427.  
  428. show_die( where, rval )
  429.  
  430. int    where, rval;
  431.  
  432.     {
  433.  
  434.     int    x_base, y_base;
  435.     int    i, mirror;
  436.  
  437.     x_base = ( where == 1 ? X_DIE1 : X_DIE2 );
  438.     y_base = ( where == 1 ? Y_DIE1 : Y_DIE2 );
  439.  
  440.     /* 2, 3, and 6 can go either way */
  441.  
  442.     mirror = rand() % 2;
  443.  
  444.     /* this is a space-consuming way of    */
  445.     /* doing this, but it is very fast on a    */
  446.     /* cp/m system.                */
  447.  
  448.     switch( rval )
  449.         {
  450.     
  451.         case 1:    write_at( x_base + 1, y_base + 2, "     ", DYE );
  452.             write_at( x_base + 2, y_base + 2, "  O  ", DYE );
  453.             write_at( x_base + 3, y_base + 2, "     ", DYE );
  454.             break;
  455.  
  456.         case 2:    if ( ! mirror )
  457.                 {
  458.                 write_at( x_base + 1, y_base + 2, "O    ", DYE );
  459.                 write_at( x_base + 2, y_base + 2, "     ", DYE );
  460.                 write_at( x_base + 3, y_base + 2, "    O", DYE );
  461.                 }
  462.             else
  463.                 {
  464.                 write_at( x_base + 1, y_base + 2, "    O", DYE );
  465.                 write_at( x_base + 2, y_base + 2, "     ", DYE );
  466.                 write_at( x_base + 3, y_base + 2, "O    ", DYE );
  467.                 }
  468.  
  469.             break;
  470.  
  471.         case 3:    if ( ! mirror )
  472.                 {
  473.                 write_at( x_base + 1, y_base + 2, "O    ", DYE );
  474.                 write_at( x_base + 2, y_base + 2, "  O  ", DYE );
  475.                 write_at( x_base + 3, y_base + 2, "    O", DYE );
  476.                 }
  477.             else
  478.                 {
  479.                 write_at( x_base + 1, y_base + 2, "    O", DYE );
  480.                 write_at( x_base + 2, y_base + 2, "  O  ", DYE );
  481.                 write_at( x_base + 3, y_base + 2, "O    ", DYE );
  482.                 }
  483.  
  484.             break;
  485.  
  486.         case 4:    write_at( x_base + 1, y_base + 2, "O   O", DYE );
  487.             write_at( x_base + 2, y_base + 2, "     ", DYE );
  488.             write_at( x_base + 3, y_base + 2, "O   O", DYE );
  489.             break;
  490.  
  491.         case 5:    write_at( x_base + 1, y_base + 2, "O   O", DYE );
  492.             write_at( x_base + 2, y_base + 2, "  O  ", DYE );
  493.             write_at( x_base + 3, y_base + 2, "O   O", DYE );
  494.             break;
  495.  
  496.         case 6:    if ( ! mirror )
  497.                 {
  498.                 write_at( x_base + 1, y_base + 2, "O   O", DYE );
  499.                 write_at( x_base + 2, y_base + 2, "O   O", DYE );
  500.                 write_at( x_base + 3, y_base + 2, "O   O", DYE );
  501.                 }
  502.             else
  503.                 {
  504.                 write_at( x_base + 1, y_base + 2, "O O O", DYE );
  505.                 write_at( x_base + 2, y_base + 2, "     ", DYE );
  506.                 write_at( x_base + 3, y_base + 2, "O O O", DYE );
  507.                 }
  508.  
  509.             break;
  510.  
  511.         } /* switch */
  512.  
  513.     } /* show_die */
  514.